package study.android.studyandroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import study.android.g;
import study.android.studyandroid.dao.DeviceDao;
import tq.android.lib.com.AJsonConvertible;
import tq.vmds.data.android.dto.TerminalInfo;
import tq.vmds.data.android.dto.TerminalInfo__test;

public class MainActivity extends Activity implements OnClickListener {
	TextView txtShowData;
	private Button btnDatabaseActivity;
	private Button btnCreateEntity;
	private Button btnLoadData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		txtShowData = (TextView) findViewById(R.id.txtShowData);
		btnDatabaseActivity = (Button) findViewById(R.id.btnDatabaseActivity);
		btnDatabaseActivity.setOnClickListener(this);
		btnCreateEntity = (Button) findViewById(R.id.btnCreateEntity);
		btnCreateEntity.setOnClickListener(this);
		btnLoadData = (Button) findViewById(R.id.btnLoadData);
		btnLoadData.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnDatabaseActivity:
			startActivity(new Intent(this, DatabaseActivity.class));
			break;
		case R.id.btnCreateEntity: {
			g.ldb.recreate();
			DeviceDao deviceDao = new DeviceDao(g.ldb);
			TerminalInfo ti = TerminalInfo__test.sample1();
			deviceDao.saveTerminalInfo(ti);
		}
			break;
		case R.id.btnLoadData: {
			DeviceDao deviceDao = new DeviceDao(g.ldb);
			TerminalInfo ti = deviceDao.fetchTerminalInfo("2131210001");
			String jsonStr = AJsonConvertible.Object2JsonString(ti);

			txtShowData.setText(jsonStr);
		}
			break;
		}
	}
}
